package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.elite.groovy.util.Util

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.dom4j.*
/**
 * @use 续保外呼，根据销管系统推送的数据，调用车承保系统获取上年保单信息
 * @author admin
 *
 */

public class GetXbSuccessInfo implements ServiceBackInvoke{
    private static final Log log=LogFactory.getLog(GetXbSuccessInfo.class);
    private String charset = "utf-8"; //设置字符集
    private int timeout = 100000; //设置请求超时时间
    private int enableSSLflag = 0;
    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        try {
			log.debug("----------GetXbSuccessInfo(续保外呼，从核心获取续保成功信息)开始----------");

			def querySql = "select t.url, t.dd_use_id, t.dd_key from SYSC_INTERFACE t where t.urltype='CX001'";
            def quelist = eliteBean.chaxun_sql(querySql, "dataSource");
            def map3 = quelist[0];
            def v_webserviceurl = map3.get("url");
            def v_stringfield5 = map3.get("dd_use_id");
            def v_token = map3.get("dd_key");


            Map headMap = new HashMap();
            headMap.put("Content-Type", "text/xml;charset=UTF-8");

            def mapn = "";
            def voctable = eliteBean.chaxun_sql("select t.relation_id RELATION_ID,case when c.CHANNELFLAG='OLS' then '000001002' else '000001001' end MerchantSubCode, c.POLICYNO BusinessNo, c.COMCODE ComCode, c.frameno VINNo  from tabc_xb_case t left join xb_cust_case c on t.relation_id =c.relation_id left join elitetask e on t.elitetask_guid = e.elitetask_guid where  e.laststatus in ('F00001','F00008','F00009','F00010') and t.enddate > to_char(add_months(sysdate,-1),'yyyy-mm-dd')", "dataSource");


            if (voctable != null && voctable.size() > 0) {
                for (def i = 0; i < voctable.size(); i++) {
                    mapn = voctable[i];
                    def RELATION_ID = mapn.get("RELATION_ID");
                    def SystemCode = "80026";
                    def ComCode = mapn.get("ComCode");  //机构
                    def MerchantCode = "000001"; //000001,直销业务
                    def MerchantSubCode = mapn.get("MerchantSubCode"); //000001001,直销业务(电销) / 000001002,直销业务(网销)
                    def BusinessNo = mapn.get("BusinessNo");  //保单号
                    def VINNo2 = mapn.get("VINNo");  //车架号
                    def url = v_webserviceurl;  //url
                    def UserCode = v_stringfield5;  //用户账号
                    def Password = v_token;  //token
					
					
					log.debug("----------GetXbSuccessInfo(续保外呼，从核心获取续保成功信息"+BusinessNo+")开始----------");
					
					
					//判断是否已续保
					def smsBody = """
                    <PACKET type="REQUEST" version="1.0">
                      <HEAD>
                        <SystemCode>$SystemCode</SystemCode>
                        <UserCode>$UserCode</UserCode>
                        <Password>$Password</Password>
                        <ComCode>$ComCode</ComCode>
                        <MerchantCode>$MerchantCode</MerchantCode>
                        <MerchantSubCode>$MerchantSubCode</MerchantSubCode>
                      </HEAD>
                      <BODY>
                        <BasePart>
                          <LicenseNo/>
                          <VINNo>$VINNo2</VINNo>
                          <PolicyNo>$BusinessNo</PolicyNo>
                        </BasePart>
                      </BODY>
                    </PACKET>
							""";
					def callBack1 = Util.doHttpPostUseHead(url, smsBody, "UTF-8", 30000, 0, headMap);		
					def xmlstr = Util.Transform(callBack1);
                    def LIST = new XmlParser().parseText(xmlstr);
					def ResponseType = LIST.HEAD.ResponseType.text();
					log.debug("----------GetXbSuccessInfo(续保外呼，从核心获取续保成功信息"+BusinessNo+"：ResponseType="+ResponseType+")开始----------");
					if (ResponseType == "1") {
					    
						def flag = 0;
						
						def risks = LIST.BODY.RiskList.Risk;
						for (def r = 0; r < risks.size(); r++) {
							def rType = LIST.BODY.RiskList.Risk[r].Type.text();
							if(rType=="2"){
								flag = 1;
							}
						}

						log.debug("flag====="+flag);
						if(flag == 1){
							String insql1 = "INSERT INTO CUSTOMERCONTACT_HIS (OBJECTIVE_GUID,COMMENTS,CREATEDDATE, CREATEDBY,  CUSTOMERCONTACT_HIS_GUID, DELFLAG, callresultreason_id ) VALUES ('"+RELATION_ID+"', '续保成功状态同步', sysdate, 'system',sysguid,  '0', 'F00000' )"
							eliteBean.update(insql1, "dataSource");
							
							String upsql = "update elitetask set laststatus = 'F00000' where objective_guid='"+RELATION_ID+"'";
							eliteBean.update(upsql, "dataSource");

                            //20241104 信创-注释
							//String upsql2 = "update tabc_xb_case set sharestatus=4,renewalflag='21' where relation_id ='"+RELATION_ID+"'";
						    //eliteBean.update(upsql2, "dataSource");
							
						}
					}
                    
                }
            }

			log.debug("----------GetXbSuccessInfo(续保外呼，从核心获取续保成功信息)结束----------");
	

        }catch(Exception e){
            log.debug("----------GetPolicyInfo(续保外呼，从核心获取续保成功信息)异常----------");
            log.error("" ,e);
        }
        return "1";
    }

}
